#!/bin/bash
#
#********************************************************************
#Author:                songliangcheng
#QQ:                    2192383945
#Date:                  2023-01-03 11:08:58
#FileName：             set +m;shopt -s lastpipe; curl -sSLf https://gitee.com/slcnx/tools/raw/master/scripts/docker/install-docker.sh | sed 's/\r//g' | script="$(</dev/stdin)"; eval "$script"
#URL:                   http://blog.mykernel.cn
#Description：          a test script
#Copyright (C):        2023 All rights reserved
#********************************************************************
#描述:
#--version 选项必须传递
source <(curl -sSLf https://gitee.com/slcnx/tools/raw/master/parse_cmd.sh | sed 's/\r//g')

CONFIG='
key    ,argument    ,isempty    ,desc    
--version    ,VERSION    ,0    ,docker版本；获取版本 https://download.docker.com/linux/static/stable/x86_64/
'
parse_cmd $@

: ${version:=$VERSION}


: ${CURRENT_DIR:="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"}
[ -f docker-$version.tgz ] || {
wget https://download.docker.com/linux/static/stable/x86_64/docker-$version.tgz
}
mkdir -pv /usr/local/docker-$version/bin/
tar xvf docker-$version.tgz --strip-components=1 -C /usr/local/docker-$version/bin/

# /etc/docker/daemon.json
install -dv /etc/docker
tee /etc/docker/daemon.json <<EOF
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2",
  "live-restore": true
}
EOF

curl -sSLf https://gitee.com/slcnx/tools/raw/master/scripts/post-compile.sh |  sed 's/\r//g' | bash -xs -- -bvp /usr/local/docker-$version/ -s dockerd

 sed  -i '/EnvironmentFile/a Environment="PATH=/usr/local/docker/bin:/usr/local/docker/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"' /etc/systemd/system/docker.service

systemctl daemon-reload
systemctl restart docker
systemctl enable docker


export PATH=/usr/local/docker/bin:$PATH
docker run --rm hello-world


cd $CURRENT_DIR
rm -f docker-$version.tgz*

# docker-compose
curl -L https://get.daocloud.io/docker/compose/releases/download/1.28.6/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose; chmod +x /usr/local/bin/docker-compose

function cri_docker_init() {
cd $CURRENT_DIR
[ -f cri-dockerd-0.3.1.amd64.tgz ] || {
  wget https://github.com/Mirantis/cri-dockerd/releases/download/v0.3.1/cri-dockerd-0.3.1.amd64.tgz
  tar xvf cri-dockerd-0.3.1.amd64.tgz --strip-components=1 -C /usr/bin/
}
cat > /etc/systemd/system/cri-docker.service <<EOF
[Unit]
Description=CRI Interface for Docker Application Container Engine
Documentation=https://docs.mirantis.com
After=network-online.target firewalld.service docker.service
Wants=network-online.target

[Service]
Type=notify
ExecStart=/usr/bin/cri-dockerd
ExecReload=/bin/kill -s HUP \$MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3

# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity

# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity
Delegate=yes
KillMode=process

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable cri-docker.service
systemctl restart cri-docker.service
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sudo sysctl --system
}

cri_docker_init
